package org.shoukaiseki.inittable.utils;


import org.shoukaiseki.common.utils.StringUtils;
import org.shoukaiseki.inittable.domain.TableColumnInfo;
import org.shoukaiseki.kakaroto.SksDictUtils;

public final class TableColumnInfoBuilder {
    private String columnName;
    private String columnDetail;
    private boolean primaryKey;

    /** 表描述 */
    private String description;

    /**
     * 注释
     */
    private String remark;


    private TableColumnInfoBuilder() {
    }

    public static TableColumnInfoBuilder column(String columnName,String... params) {

        TableColumnInfoBuilder tableColumnInfoBuilder = new TableColumnInfoBuilder();
        String str = StringUtils.format(columnName,params).trim();
        if (str.lastIndexOf(",")==str.length()-1) {
            str=str.substring(0,str.length()-1);
        }
        int i = str.indexOf(" ");
        tableColumnInfoBuilder.columnName=str.substring(0,i).trim();
        tableColumnInfoBuilder.columnDetail=str.substring(i).trim();
        return tableColumnInfoBuilder;
    }

    public TableColumnInfoBuilder columnName(String columnName) {
        this.columnName = columnName;
        return this;
    }

    public TableColumnInfoBuilder columnDetail(String columnDetail) {
        this.columnDetail = columnDetail;
        return this;
    }

    public TableColumnInfoBuilder primaryKey(){
        this.primaryKey=true;
        return this;
    }


    //取字典枚举类中的描述
    public TableColumnInfoBuilder description(Class<?> clazz) {
        this.description = SksDictUtils.getDictInfo(clazz);
        return this;
    }

    public TableColumnInfoBuilder description(String description) {
        this.description = description;
        return this;
    }

    public TableColumnInfoBuilder remark(String remark) {
        this.remark = remark;
        return this;
    }

    public TableColumnInfo build() {
        TableColumnInfo tableColumnInfo = new TableColumnInfo();
        tableColumnInfo.setColumnName(columnName);
        tableColumnInfo.setColumnDetail(columnDetail);
        tableColumnInfo.setPrimaryKey(primaryKey);
        tableColumnInfo.setDescription(description);
        tableColumnInfo.setRemark(remark);
        return tableColumnInfo;
    }
}
